package com.bingo.demo.controller;

import com.bingo.demo.pojo.Essay;
import com.bingo.demo.pojo.User;
import com.bingo.demo.service.EssayService;
import com.bingo.demo.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    EssayService essayService;
    @RequestMapping("login")
    public String login(String username, String password, Model model){
        //获取当前请求登录的用户
        Subject subject= SecurityUtils.getSubject();
        //封装登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            //采用封装好的数据进行登录
            subject.login(token);
            //拿到登录成功的用户对象
            User user = (User)subject.getPrincipal();
            if(user.getName().length()>2){
                String newName = user.getName().substring(0, 2);
                user.setName(newName);
            }
            Session session = subject.getSession();
            session.setAttribute("loginUser",user);
            List<Essay> essayList= essayService.findByAuthoruty(1);
            //只返回5条数据
            if(essayList.size() > 5){
                List<Essay> list = essayList.subList(0, 5);
                session.setAttribute("essayList",list);
                session.setAttribute("hasNext",1);
            }else {
                session.setAttribute("essayList",essayList);
                session.setAttribute("hasNext",0);
            }
            session.setAttribute("hasLast",0);
            session.setAttribute("pageNum",1);
            return "index";
        }catch (UnknownAccountException usa){
            //用户名错误处理
            model.addAttribute("msg","用户名错误");
            return "login";
        }catch (IncorrectCredentialsException ice){
            //密码错误处理
            model.addAttribute("msg","密码错误");
            return "login";
        }catch (LockedAccountException lae){
            //用户登录权限处理
            return null;
        }
    }

    @RequestMapping("register.action")
    public String register(HttpServletRequest request,Model model){
        String username = request.getParameter("username");
        User userByUsername = userService.findUserByUsername(username);
        if(userByUsername!=null){
            model.addAttribute("respon","用户名已存在");
            return "register";
        }
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String phoneNum = request.getParameter("phoneNum");
        String personMsg = request.getParameter("personMsg");
        String sex = request.getParameter("sex");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setName(name);
        user.setSex(sex);
        user.setPersonMessage(personMsg);
        user.setPhoneNum(phoneNum);
        userService.addUser(user);
        return "login";
    }

    @PostMapping("updateUser")
    @ResponseBody
    public void updateUser(HttpServletRequest request, HttpSession session){
        String id = request.getParameter("userId");
        User user = userService.findUserById(Integer.parseInt(id));
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String personMessage = request.getParameter("personMessage");
        String phoneNum = request.getParameter("phoneNum");
        if(name != null){
            user.setName(name);
            System.out.println(name+id);
        }else if(age != null){
            user.setAge(Integer.parseInt(age));
        }else if(personMessage != null){
            user.setPersonMessage(personMessage);
        }else if(phoneNum != null){
            user.setPhoneNum(phoneNum);
        }
        userService.updateUser(user);
        session.setAttribute("loginUser",user);
    }

}
